﻿using System;
using Birkelund.QueryStrings.Internals;
using Birkelund.QueryStrings;
using System.Diagnostics;

namespace Birkelund.QueryStrings
{
    /// <summary>
    /// Configuration class for the QueryString framework
    /// </summary>
    public sealed class QueryStringConfiguration
    {
        private static readonly QueryStringConfiguration _instance = new QueryStringConfiguration();

        private QueryStringConfiguration()
        {
            ChecksumCalculator = ChecksumCalculators.Basic; // assign to default value
        }

        /// <summary>
        /// Returns the singleton instance of the configuration
        /// </summary>
        public static QueryStringConfiguration Instance { get { return _instance; } }
        
        #region FullEncryption
        internal ISymmetricEncryption Encryptor { get; set; }

        /// <summary>
        /// Removed any full encryption
        /// </summary>
        public void RemoveFullEncryption()
        {
            Encryptor = null;
        }

        /// <summary>
        /// Set a custom crypto provider to be used for encrypt/decrypt entire querystring
        /// </summary>
        /// <param name="encryptor">The symmetric encryption algorithm</param>
        public void SetFullEncryption(ISymmetricEncryption encryptor)
        {
            if (encryptor == null)
            {
                RemoveFullEncryption();
            }
            else
            {
                Encryptor = encryptor;
            }
        }

        /// <summary>
        /// Using the default cryotp provider to encrypt/decrypt entire querystring
        /// </summary>
        public void SetFullEncryption()
        {
            SetFullEncryption(new DesEncryptionImpl());
        } 
        #endregion

        #region ChecksumAlgorithm
        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
        private IChecksumCalculator _checksumCalculator;
        
        /// <summary>
        /// Gets or sets the calculate check sum algorithm.
        /// </summary>
        /// <value>The calculate check sum algorithm.</value>
        public IChecksumCalculator ChecksumCalculator
        {
            get { return _checksumCalculator; }
            set
            {
                ArgumentValidation.ThrowIfNull(value, "Null is not a valid value for ChecksumCalculator");
                _checksumCalculator = value;
            }
        }
        #endregion

        //public string RedirectToUriOnError { get; set; }
    }
}
